/* GUI CONVERTED from apvomtka.p (converter v1.76) Tue Mar 18 16:13:24 2003 */
/* apvomtka.p - AP VOUCHER MAINTENANCE BACK OUT COST UPDATES ON INVTY DB      */
/* Copyright 1986-2003 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.17 $                                               */
/*V8:ConvertMode=Maintenance                                                  */
/* REVISION: 9.0            CREATED: 03/15/99   BY: *M0BG* Jeff Wootton       */
/* REVISION: 9.1           MODIFIED: 10/01/99   BY: *N014* Jeff Wootton       */
/* REVISION: 9.1           MODIFIED: 11/01/99   BY: *N053* Jeff Wootton       */
/* REVISION: 9.1           MODIFIED: 08/11/00   BY: *N0KK* jyn                */
/* REVISION: 9.1      LAST MODIFIED: 01/17/01   BY: *M0ZL* Atul Dhatrak       */
/* Revision: 1.7      BY: Santosh Rao         DATE: 02/20/02  ECO: *N190*     */
/* Revision: 1.8      BY: Rajiv Ramaiah       DATE: 03/20/02  ECO: *M1WW*     */
/* Revision: 1.9      BY: Robin McCarthy      DATE: 04/05/02  ECO: *P000*     */
/* Revision: 1.10     BY: Patrick Rowan       DATE: 04/29/02  ECO: *P05Q*     */
/* Revision: 1.14     BY: Manjusha Inglay     DATE: 07/29/02  ECO: *N1P4*     */
/* Revision: 1.15     BY: Samir Bavkar        DATE: 08/16/02  ECO: *P0FM*     */
/* Revision: 1.16     BY: Jose Alex    DATE: 08/19/02  ECO: *M1XR* */
/* $Revision: 1.17 $       BY: Tiziana Giustozzi   DATE: 01/13/03 ECO: *P0MX* */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/************************************************************************/
/* This is run from apvomtk1.p, connected to the inventory database     */
/************************************************************************/

{mfdeclre.i}

{gldydef.i new} /* DEFINE NEW INSTANCE ON DAYBOOKS VARIABLES */
{gldynrm.i new} /* DEFINE NEW INSTANCE OF NRM FOR THIS DATABASE */

define input        parameter p_ap_database          like si_db.
define input        parameter p_ap_ref               like ap_ref.
define input        parameter p_ap_acct              like ap_acct.
define input        parameter p_ap_sub               like ap_sub.
define input        parameter p_ap_cc                like ap_cc.
define input        parameter p_pod_wo_lot           like pod_wo_lot.
define input        parameter p_pod_op               like pod_op.
define input        parameter p_pvo_part             like pvo_part.
define input        parameter p-po-site              like pvo_shipto.
define input        parameter p_pvo_shipto           like pvo_shipto.
define input        parameter p-receipt-type         like pod_type.
define input        parameter p_pvo_cost_update      like pvo_cost_update.
define input        parameter p-um-conv              like pod_um_conv.
define input        parameter p_pvo_trans_qty        like pvo_trans_qty.
define input        parameter p_pvo_order            like pvo_order.
define input        parameter p_pvo_project          like pvo_project.

define input        parameter p_vph_adj_amt          like vph_adj_amt.
define input        parameter p_vph_adj_prv_cst      like vph_adj_prv_cst.
define input        parameter p_vph_dscr_amt         like vph_dscr_amt.

define input        parameter p_vph_costadj_acct     like vph_costadj_acct.
define input        parameter p_vph_costadj_sub      like vph_costadj_sub.
define input        parameter p_vph_costadj_cc       like vph_costadj_cc.
define input        parameter p_inv_yn               like mfc_logical.
define input        parameter p_wip_yn               like mfc_logical.
define input        parameter p_totl_amt             like glt_amt.
define input        parameter p_unit_amt             like glt_amt.
define input        parameter p_qty_oh_at_prev_adj   like in_qty_oh.

define input-output parameter p_vph_cf_adj_amt       like vph_cf_adj_amt.
define input-output parameter p_vph_cf_dscr_amt      like vph_cf_dscr_amt.

define input-output parameter p_vph_dscr_acct        like vph_dscr_acct.
define input-output parameter p_vph_dscr_sub         like vph_dscr_sub.
define input-output parameter p_vph_dscr_cc          like vph_dscr_cc.

define variable glx_mthd          like cs_method.
define variable glx_set           like cs_set.
define variable cur_mthd          like cs_method.
define variable cur_set           like cs_set.
define variable qty_oh            like in_qty_oh.
define variable qty_po            like pvo_trans_qty.
define variable oldcst            like sct_cst_tot.
define variable curflag           like mfc_logical.
define variable glxflag           like mfc_logical.
define variable pct_oh              as decimal.
define variable gl_tmp_amt          as decimal.
define variable l_inv_amt           as decimal          no-undo.
define variable l_discr_amt         as decimal          no-undo.
define variable l_gl_ref          like glt_ref          no-undo.
define variable mc-error-number   like msg_nbr          no-undo.
define variable l_vph_cf_adj_amt  like vph_cf_adj_amt   no-undo.
define variable l_vph_cf_dscr_amt like vph_cf_dscr_amt  no-undo.


define new shared variable ref      like glt_ref.
define new shared variable eff_date like glt_effdate initial today.

for first in_mstr
   fields(in_qty_oh
   in_gl_cost_site in_gl_set
   in_qty_nonet)
   where in_part = p_pvo_part
     and in_site = p_pvo_shipto
   no-lock:
end. /* FOR FIRST in_mstr */
if not available in_mstr
then
   return.

for first gl_ctrl
   fields(gl_rnd_mthd)
   no-lock:
end. /* FOR FIRST gl_ctrl */

for first icc_ctrl
   fields(icc_cur_ap)
   no-lock:
end. /* FOR FIRST icc_ctrl */

assign
   qty_oh  = in_qty_oh + in_qty_nonet
   curflag = no
   glxflag = no.

/*DETERMINE COSTING METHOD*/
{gprun.i ""csavg01.p""
   "(input p_pvo_part,
     input p_pvo_shipto,
     output glx_set,
     output glx_mthd,
     output cur_set,
     output cur_mthd
     )"}
/*GUI*/ if global-beam-me-up then undo, leave.


 
/* REVERSE CURRENT INVENTORY COST WHEN POSTED TO VARIANCE */

if  p-receipt-type = ""
and p_pvo_cost_update  /* PO, UPDATE AVG/LAST COST = YES */
and icc_cur_ap     /* IC CONTROL, CURRENT COST FROM AP = YES */
and (cur_mthd = "AVG" or cur_mthd = "LAST")
and (glx_mthd <> "AVG")
then do:
   {gpsct01.i &set=cur_set &part=p_pvo_part &site=p_pvo_shipto}
   assign
      curflag = yes
      curcst  = sct_mtl_tl.

   if qty_oh <= 0
   then
      /* RESTORE PREVIOUS COST */
      1 = 1.
      /*sct_mtl_tl = p_vph_adj_prv_cst. tony */
   else
   if cur_mthd = "AVG"
   then do:
      assign
         /* CALC PORTION OF CURRENT QOH                           */
         /* COMPARED TO QOH AS OF PREVIOUS ADJUSTMENT             */
         /* TO AVOID DIVIDE BY ZERO ERROR, ASSIGN pct_oh = 1 WHEN */
         /* PREVIOUS QOH IS ZERO                                  */
         pct_oh = if (p_qty_oh_at_prev_adj = 0)
                  then
                     1
                  else
                     min(1,qty_oh / p_qty_oh_at_prev_adj)
         /* CALC NEW AVERAGE COST                               */
         sct_mtl_tl = ((sct_mtl_tl * qty_oh)
                    + (pct_oh * p_totl_amt))
                    / qty_oh.

      if sct_mtl_tl <= 0
      then
         sct_mtl_tl = curcst.
   end. /* IF cur_mthd = "AVG" */
   if sct_mtl_tl <> curcst
   then
      assign
         sct_cst_tot  = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                      + sct_ovh_tl + sct_sub_tl + sct_mtl_ll
                      + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                      + sct_sub_ll
         sct_cst_date = today.
end. /* IF p-receipt-type = "" AND .. */
else

/* REVERSE CURRENT SUBCONTRACT COST WHEN POSTED TO VARIANCE */

if  p-receipt-type = "S"
and p_pvo_cost_update  /* PO, UPDATE AVG/LAST COST = YES */
and icc_cur_ap     /* IC CONTROL, CURRENT COST FROM AP = YES */
and (cur_mthd = "AVG" or cur_mthd = "LAST")
and (glx_mthd <> "AVG")
then do:
   for first wr_route
      fields /* (none) */
      where wr_lot = p_pod_wo_lot
        and wr_op  = p_pod_op
      no-lock:
   end. /* FOR FIRST wr_route */
   if not available wr_route
   then do:
      {gpsct01.i &set=cur_set &part=p_pvo_part &site=p_pvo_shipto}
      assign
         curflag = yes
         curcst  = sct_sub_tl.

      if qty_oh <= 0
      then
		   1 = 1.
         /* RESTORE PREVIOUS COST */
        /* sct_sub_tl = p_vph_adj_prv_cst. tony */
      else
      if cur_mthd = "AVG"
      then
         assign
            /* CALC PORTION OF CURRENT QOH                           */
            /* COMPARED TO QOH AS OF PREVIOUS ADJUSTMENT             */
            /* TO AVOID DIVIDE BY ZERO ERROR, ASSIGN pct_oh = 1 WHEN */
            /* PREVIOUS QOH IS ZERO                                  */
            pct_oh = if (p_qty_oh_at_prev_adj = 0)
                     then
                        1
                     else
                        min(1,qty_oh / p_qty_oh_at_prev_adj)
            sct_sub_tl = ((sct_sub_tl * qty_oh)
                       + (pct_oh * p_totl_amt)) / qty_oh.

      if sct_sub_tl <= 0
      then
         sct_sub_tl = curcst.
      if sct_sub_tl <> curcst
      then
         assign
            sct_cst_tot = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                        + sct_ovh_tl + sct_sub_tl + sct_mtl_ll
                        + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                        + sct_sub_ll
            sct_cst_date = today.
   end. /* IF NOT AVAILABLE wr_route THEN */
end. /* IF p-receipt-type = "S" AND .. */
else
/* REVERSE - CURRENT & GL SUBCONTRACT COST WHEN POSTED TO WIP */
if  p-receipt-type = "S"
and p_wip_yn
and (glx_mthd = "AVG" or cur_mthd = "AVG" or cur_mthd = "LAST")
then do:
   for first wo_mstr
      where wo_lot = p_pod_wo_lot
      exclusive-lock:

      if not wo_acct_close
      then
         for first wr_route
            where wr_lot = p_pod_wo_lot
              and wr_op  = p_pod_op
            exclusive-lock:
            assign
               wr_sub_act = wr_sub_act + p_totl_amt
               wo_wip_tot = wo_wip_tot + p_totl_amt
               wo_sub_tot = wo_sub_tot + p_totl_amt.
         end. /* FOR FIRST wr_route */
   end. /* FOR FIRST wo_mstr */

   if not available wo_mstr
   or wo_acct_close
   or not available wr_route
   then
      assign
         l_inv_amt   = 0
         l_discr_amt = p_totl_amt.

   /* ADJUST CONFIRMED ADJUSTMENT AMOUNT               */
   /* AND CONFIRMED DISCREPANCY AMOUNT                 */
   /* BY THE AMOUNT OF REVERSED ADJUSTMENT WHICH COULD */
   /* NOT BE APPLIED TO WIP DUE TO NOT FOUND OR CLOSED */
   assign
      p_vph_cf_adj_amt  = p_vph_cf_adj_amt + l_discr_amt
      p_vph_cf_dscr_amt = p_vph_cf_dscr_amt - l_discr_amt.
end. /* IF p-receipt-type = "S" AND .. */
else
/* REVERSE CURRENT & GL INVENTORY COST WHEN POSTED TO INVENTORY */
if  p-receipt-type = ""
and p_inv_yn
and (glx_mthd = "AVG" or cur_mthd = "AVG" or cur_mthd = "LAST")
and p_totl_amt <> 0
then do:

   if p_vph_dscr_acct = ""
   then do:
      /* SAVE INVENTORY DISCREPANCY ACCOUNT AND COST CENTER */
      for first pt_mstr
         fields(pt_prod_line)
         where pt_part = p_pvo_part
         no-lock:

         for first pl_mstr
            fields(pl_dscr_acct pl_dscr_sub pl_dscr_cc)
            where pl_prod_line = pt_prod_line
            no-lock:
         end. /* FOR FIRST pl_mstr */

         for first pld_det
            fields(pld_dscracct pld_dscr_sub pld_dscr_cc)
            where pld_prodline = pt_prod_line
              and pld_site     = p_pvo_shipto
              and pld_loc      = ""
            no-lock:
         end. /* FOR FIRST pld_det */
         if not available pld_det
         then do:
            for first pld_det
               fields(pld_dscracct pld_dscr_sub pld_dscr_cc)
               where pld_prodline = pt_prod_line
                 and pld_site     = ""
                 and pld_loc      = ""
               no-lock:
            end. /* FOR FIRST pld_det */
         end. /* IF NOT AVAILABLE pld_det */
      end. /* FOR FIRST pt_mstr */

      if available pld_det
      then
         assign
            p_vph_dscr_acct = pld_dscracct
            p_vph_dscr_sub  = pld_dscr_sub
            p_vph_dscr_cc   = pld_dscr_cc.
      else
         assign
            p_vph_dscr_acct = pl_dscr_acct
            p_vph_dscr_sub  = pl_dscr_sub
            p_vph_dscr_cc   = pl_dscr_cc.
   end. /* IF p_vph_dscr_amt = "" THEN */

   assign
      qty_po = p_pvo_trans_qty * p-um-conv
      /* CALC PORTION OF CURRENT QOH                           */
      /* COMPARED TO QOH AS OF PREVIOUS ADJUSTMENT             */
      /* TO AVOID DIVIDE BY ZERO ERROR, ASSIGN pct_oh = 1 WHEN */
      /* PREVIOUS QOH IS ZERO                                  */
      pct_oh = if (p_qty_oh_at_prev_adj = 0)
               then
                  1
               else
                  min(1,qty_oh / p_qty_oh_at_prev_adj)
      /* CALC PORTION OF ADJUSTMENT APPLICABLE TO CURRENT QOH  */
      l_inv_amt = pct_oh * ( - p_vph_adj_amt).

   {gprunp.i "mcpl" "p" "mc-curr-rnd"
      "(input-output l_inv_amt,
        input gl_rnd_mthd,
        output mc-error-number)"}
   if mc-error-number <> 0
   then do:
      {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
   end. /* IF mc-error-number <> 0 */

   /* CALC PORTION OF ADJUSTMENT WHICH IS A DISCREPANCY */
   l_discr_amt = p_totl_amt - l_inv_amt.

   if qty_oh > 0
   then do:
      {gpsct01.i &set=glx_set &part=p_pvo_part &site=p_pvo_shipto}

      assign
         glxflag    = yes
         glxcst     = sct_mtl_tl
         oldcst     = sct_cst_tot

         /* WHEN VOCUHER IS DELETED, THE AVERAGE GL COST SHOULD BE */
         /* RESTORED BASED ON THE AMOUNT POSTED TO THE INVENTORY   */
         /* ACCOUNT AND NOT BY THE TOTAL RATE VARIANCE.            */
         sct_mtl_tl = ((sct_mtl_tl * qty_oh)
                    + (pct_oh * ( - p_vph_adj_amt))) / qty_oh.

      if sct_mtl_tl <= 0
      then
         assign
            sct_mtl_tl  = glxcst
            l_inv_amt   = 0
            l_discr_amt = p_totl_amt.

      if sct_mtl_tl <> glxcst
      then do:
         assign
            sct_cst_tot  = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                           + sct_ovh_tl + sct_sub_tl + sct_mtl_ll
                           + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                           + sct_sub_ll
            sct_cst_date = today.

         /* CREATE REVERSAL TR_HIST AND TRGL_DET */

         {ictrans.i
            &addrid=""""
            &bdnstd=0
            &cracct=p_ap_acct
            &crsub=p_ap_sub
            &crcc=p_ap_cc
            &crproj=p_pvo_project
            &curr=""""
            &dracct=p_vph_costadj_acct
            &drsub=p_vph_costadj_sub
            &drcc=p_vph_costadj_cc
            &drproj=p_pvo_project
            &effdate=eff_date
            &exrate=0
            &exrate2=0
            &exratetype=""""
            &exruseq=0
            &glamt=0
            &lbrstd=0
            &line=0
            &location=""""
            &lotnumber=""""
            &lotref=""""
            &lotserial=""""
            &mtlstd=0
            &ordernbr=p_pvo_order
            &ovhstd=0
            &part=p_pvo_part
            &perfdate=?
            &price="sct_cst_tot - oldcst"
            &quantityreq=qty_po
            &quantityshort=0
            &quantity=0
            &revision=""""
            &rmks=""""
            &shiptype=""""
            &site=p_pvo_shipto
            &slspsn1=""""
            &slspsn2=""""
            &sojob=""""
            &substd=0
            &transtype=""CST-ADJ""
            &msg=5207
            &ref_site=tr_site
         }

         assign
            tr_gl_amt    = l_inv_amt
            trgl_gl_amt  = l_inv_amt
            trgl_gl_ref  = p_ap_ref
            trgl_type    = "RCT-AVG"
            trgl_dy_code = dft-daybook
            trgl_dy_num  = nrm-seq-num.

         /*UPDATE CURRENT COST*/
         {gpsct01.i &set=cur_set &part=p_pvo_part &site=p_pvo_shipto}
         assign
            curflag = yes
            curcst  = sct_mtl_tl.

         if qty_oh <= 0
         then
            sct_mtl_tl = sct_mtl_tl + p_unit_amt.
         else
         if cur_mthd = "AVG"
         then
            sct_mtl_tl = ((sct_mtl_tl * qty_oh) + (pct_oh * p_totl_amt))
                         / qty_oh.

         if sct_mtl_tl <= 0
         then
            sct_mtl_tl = curcst.
         if sct_mtl_tl <> curcst
         then
            assign
               sct_cst_tot  = sct_mtl_tl + sct_lbr_tl + sct_bdn_tl
                            + sct_ovh_tl + sct_sub_tl + sct_mtl_ll
                            + sct_lbr_ll + sct_bdn_ll + sct_ovh_ll
                            + sct_sub_ll
               sct_cst_date = today.

      end. /* IF sct_mtl_tl <> glxcst */
   end. /* IF qty_oh > 0 */

   /* ADJUST CONFIRMED ADJUSTMENT AMOUNT               */
   /* AND CONFIRMED DISCREPANCY AMOUNT                 */
   /* BY THE AMOUNT OF REVERSED ADJUSTMENT WHICH COULD */
   /* NOT BE APPLIED TO INVENTORY DUE TO LOW QOH       */

   assign
      p_vph_cf_adj_amt  = p_vph_cf_adj_amt + l_discr_amt
      p_vph_cf_dscr_amt = p_vph_cf_dscr_amt - l_discr_amt.

end. /* REVERSE CURRENT & GL INVENTORY COST */

/* STORING VALUES FROM p_vph_cf_adj_amt & p_vph_cf_dscr_amt  */
/* INTO l_vph_cf_adj_amt & l_vph_cf_dscr_amt RESPECTIVELY    */
/* AND THEN ROUNDING THESE VALUES SO THAT ROUNDED VALUES ARE */
/* PASSED AS INPUT PARAMETERS FOR CALLING apvoglu1.p         */

assign
   l_vph_cf_adj_amt  = p_vph_cf_adj_amt
   l_vph_cf_dscr_amt = p_vph_cf_dscr_amt.

{gprunp.i "mcpl" "p" "mc-curr-rnd"
   "(input-output l_vph_cf_adj_amt,
     input gl_rnd_mthd,
     output mc-error-number)"}

if mc-error-number <> 0
then do:
   {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
end. /* IF mc-error-number <> 0 THEN */

{gprunp.i "mcpl" "p" "mc-curr-rnd"
   "(input-output l_vph_cf_dscr_amt,
     input gl_rnd_mthd,
     output mc-error-number)"}

if mc-error-number <> 0
then do:
   {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
end. /* IF mc-error-number <> 0 THEN */

/* IF CONFIRMED COST ADJUSTMENT AMOUNT                      */
/* OR CONFIRMED COST ADJUSTMENT DISCREPANCY AMOUNT          */
/* ARE DIFFERENT FROM VOUCHER,                              */
/*   CREATE IC-TYPE GL TRANSACTIONS ON INVENTORY DATABASE.  */

/* CREATE "IC" GL FOR CONFIRMED COST ADJUSTMENT */

if p_vph_adj_amt  <> 0
or p_vph_dscr_amt <> 0
then do:
   /* CHANGED SIXTEENTH INPUT PARAMETER FROM         */
   /* p_vph_cf_adj_amt TO l_vph_cf_adj_amt           */
   /* CHANGED SEVENTEENTH INPUT PARAMETER FROM       */
   /* p_vph_cf_dscr_amt TO l_vph_cf_dscr_amt         */

   if glx_mthd = "AVG"
   then do:
      {gprun.i ""apvoglu1.p""
      "(input ""INV"",
        input ""DEL"",
        input p_ap_database,
        input p-po-site,
        input p_pvo_shipto,
        input p_vph_costadj_acct,
        input p_vph_costadj_sub,
        input p_vph_costadj_cc,
        input p_vph_dscr_acct,
        input p_vph_dscr_sub,
        input p_vph_dscr_cc,
        input p_pvo_project,
        input p_pvo_order,
        input p_vph_adj_amt,
        input p_vph_dscr_amt,
        input l_vph_cf_adj_amt,
        input l_vph_cf_dscr_amt,
        output l_gl_ref
        )"}
/*GUI*/ if global-beam-me-up then undo, leave.

   end. /* IF glx_mthd = "AVG" */

   /* STORE IC GL REF IN IC TRANSACTION IF AVAILABLE */
   /* THIS IS MORE USEFUL THAN THE VOUCHER NUMBER STORED EARLIER */
   if  available trgl_det
   and l_gl_ref <> ""
   then
      trgl_gl_ref = l_gl_ref.
end. /* IF p_vph_adj_amt <> 0 OR .. */

if curflag
then do:
   {gpsct01.i &set=cur_set &part=p_pvo_part &site=p_pvo_shipto}
   for each spt_det
      where spt_site = sct_site
        and spt_sim  = sct_sim
        and spt_part = sct_part
      exclusive-lock:

      if spt_pct_ll = 1
      then
         assign
            spt_cst_tl = sct_mtl_tl
            spt_cst_ll = sct_mtl_ll.
      else
      if spt_pct_ll = 2
      then
         assign
            spt_cst_tl = sct_lbr_tl
            spt_cst_ll = sct_lbr_ll.
      else
      if spt_pct_ll = 3
      then
         assign
            spt_cst_tl = sct_bdn_tl
            spt_cst_ll = sct_bdn_ll.
      else
      if spt_pct_ll = 4
      then
         assign
            spt_cst_tl = sct_ovh_tl
            spt_cst_ll = sct_ovh_ll.
      else
      if spt_pct_ll = 5
      then
         assign
            spt_cst_tl = sct_sub_tl
            spt_cst_ll = sct_sub_ll.
      else
      /* DO NOT ZERO OUT COSTS FOR THE LOGISTICS CHARGE COST ELEMENT */
      if not can-find(first lc_mstr where lc_element = spt_element) then
         assign
            spt_cst_tl = 0
            spt_cst_ll = 0.
   end. /* FOR EACH spt_det */
end. /* IF curflag */

if glxflag
then do:
   {gpsct01.i &set=glx_set &part=p_pvo_part &site=p_pvo_shipto}
   for each spt_det
      where spt_site = sct_site
        and spt_sim  = sct_sim
        and spt_part = sct_part
      exclusive-lock:

      if spt_pct_ll = 1
      then
         assign
            spt_cst_tl = sct_mtl_tl
            spt_cst_ll = sct_mtl_ll.
      else
      if spt_pct_ll = 2
      then
         assign
            spt_cst_tl = sct_lbr_tl
            spt_cst_ll = sct_lbr_ll.
      else
      if spt_pct_ll = 3
      then
         assign
            spt_cst_tl = sct_bdn_tl
            spt_cst_ll = sct_bdn_ll.
      else
      if spt_pct_ll = 4
      then
         assign
            spt_cst_tl = sct_ovh_tl
            spt_cst_ll = sct_ovh_ll.
      else
      if spt_pct_ll = 5
      then
         assign
            spt_cst_tl = sct_sub_tl
            spt_cst_ll = sct_sub_ll.
      else
         assign
            spt_cst_tl = 0
            spt_cst_ll = 0.
   end. /* FOR EACH spt_det */
end. /* IF glxflag */
